home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’92 / Text Capture FKEY / Copy text.c < prev    next >
C/C++ Source or Header  |  1992-06-12  |  2KB  |  93 lines

  1. #include "Copy text.h"
  2. #include "defs.h"
  3. #include <Script.h>
  4.  
  5. static void Insert_text( short byteCnt, Ptr textAddr );
  6.  
  7. void    Copy_all_text( short byteCnt, Ptr textAddr )
  8. {
  9.     Insert_text( byteCnt, textAddr );
  10. }
  11.  
  12. void    Copy_window_text( short byteCnt, Ptr textAddr )
  13. {
  14.     if ( PtInRect(front->pnLoc, &front->portRect) )
  15.     {
  16.         Insert_text( byteCnt, textAddr );
  17.     }
  18. }
  19.  
  20. void    Copy_selected_text( short byteCnt, Ptr textAddr )
  21. {
  22.     short        first_offset, last_offset;
  23.     short        widths[500];
  24.     register short    i;
  25.     short        pen_left;
  26.     
  27.     if ( (front->pnLoc.v < selected_rect.top) ||
  28.         (front->pnLoc.v > selected_rect.bottom) )
  29.         return;
  30.     
  31.     SetPort( front );
  32.     pen_left = front->pnLoc.h;
  33.     if (byteCnt < 500)
  34.     {
  35.         MeasureText( byteCnt, (char *)textAddr, widths );
  36.         i = 0;
  37.         while ( (i < byteCnt) && (pen_left + widths[i] < selected_rect.left) )
  38.             ++i;
  39.         first_offset = i;
  40.         i = byteCnt;
  41.         while ( (i > 0) && (pen_left + widths[i] > selected_rect.right) )
  42.             --i;
  43.         last_offset = i;
  44.     }
  45.     else
  46.     {
  47.         first_offset = 0;
  48.         last_offset = byteCnt;
  49.     }
  50.  
  51.     SetPort( text_port );
  52.     
  53.     if (first_offset < last_offset)
  54.     {
  55.         Insert_text( last_offset - first_offset, textAddr + first_offset );
  56.     }
  57. }
  58.  
  59. static void Insert_text( short byteCnt, Ptr textAddr )
  60. {
  61.     short    length;
  62.     TextStyle        style_rec;
  63.     
  64.     if (last_v == 0)    // This is the first time text has been drawn
  65.         last_v = front->pnLoc.v;
  66.     else                // Separate this text from previous text
  67.     {
  68.         if ( spaces || (front->pnLoc.v == last_v) )
  69.             TEKey( ' ', text_h );
  70.         else
  71.         {
  72.             TEKey( 0x0D /* return */, text_h );
  73.             last_v = front->pnLoc.v;
  74.         }
  75.         
  76.         /* Make the space just inserted unstyled. */
  77.         length = (**text_h).teLength;
  78.         TESetSelect( length - 1, length, text_h );
  79.         style_rec.tsFace = 0;
  80.         TESetStyle( doFace, &style_rec, false, text_h );
  81.         TESetSelect( length, length, text_h );
  82.     }
  83.  
  84.     TEInsert( textAddr, (long)byteCnt, text_h );
  85.     length = (**text_h).teLength;
  86.     TESetSelect( length - byteCnt, length, text_h );
  87.     style_rec.tsFont = front->txFont;
  88.     style_rec.tsFace = front->txFace;
  89.     style_rec.tsSize = front->txSize;
  90.     TESetStyle( doFont+doFace+doSize, &style_rec, false, text_h );
  91.     TESetSelect( length, length, text_h );
  92. }
  93.